package org.msfj.admin.org.controller;

import org.msfj.admin.model.Menu;
import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.model.User;
import org.msfj.admin.vo.MenuVo;
import org.msfj.admin.model.Menu;
import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.model.User;
import org.msfj.admin.vo.MenuVo;
import org.msfj.admin.model.Menu;
import org.msfj.admin.model.RoleMenu;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.MenuService;
import org.msfj.admin.vo.MenuVo;
import org.msfj.common.base.BaseController;
import org.msfj.common.model.CascaderResult;
import org.msfj.common.model.CommonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 系统菜单信息controller
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@Api(value = "系统菜单信息接口", tags = "系统菜单信息接口")
@RestController
@RequestMapping("org/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    /**
     * 添加系统菜单信息
     *
     * @param menuVo
     * @return CommonResult.ok(MenuVo)
     */
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加系统菜单信息", notes = "添加系统菜单信息")
    @ApiImplicitParam(value = "menuVo", name = "menuVo", dataType = "org.msfj.admin.vo.MenuVo", paramType = "query")
    public CommonResult addMenu(MenuVo menuVo) {
        Menu menu = menuService.saveOrEdit(menuVo);
        return CommonResult.ok(menuService.setOtherProperties(menu));
    }

    /**
     * 修改系统菜单信息
     *
     * @param menuVo
     * @return CommonResult.ok(MenuVo)
     */
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "修改系统菜单信息", notes = "修改系统菜单信息")
    @ApiImplicitParam(value = "menuVo", name = "menuVo", dataType = "org.msfj.admin.vo.MenuVo", paramType = "query")
    public CommonResult editMenu(MenuVo menuVo) {
        Menu menu = menuService.saveOrEdit(menuVo, User.class);
        return CommonResult.ok(menuService.setOtherProperties(menu));
    }

    /**
     * 逻辑删除系统菜单信息
     *
     * @param menuVo
     * @return CommonResult.ok(MenuVo)
     */
    @PostMapping("/del")
    @ResponseBody
    @ApiOperation(value = "逻辑删除系统菜单信息", notes = "逻辑删除系统菜单信息")
    @ApiImplicitParam(value = "menuVo", name = "menuVo", dataType = "org.msfj.admin.vo.MenuVo", paramType = "query")
    public CommonResult delMenu(MenuVo menuVo) {
        menuService.delById(menuVo, User.class, RoleMenu.class);
        return CommonResult.ok();
    }

    /**
     * 逻辑删除系统用户信息
     *
     * @param ids
     * @return CommonResult.ok()
     */
    @PostMapping("/dels")
    @ResponseBody
    @ApiOperation(value = "逻辑删除系统菜单信息", notes = "逻辑删除系统菜单信息")
    @ApiImplicitParam(value = "ids", name = "ids", dataType = "java.util.List", paramType = "query")
    public CommonResult delUsers(@RequestParam("ids[]") List<String> ids) {
        Menu menu = new Menu();
        menuService.delByIds(ids, menu);
        return CommonResult.ok();
    }

    /**
     * 真实删除系统菜单信息
     *
     * @param menuVo
     * @return CommonResult.ok()
     */
    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "真实删除系统菜单信息", notes = "真实删除系统菜单信息")
    @ApiImplicitParam(value = "menuVo", name = "menuVo", dataType = "org.msfj.admin.vo.MenuVo", paramType = "query")
    public CommonResult deleteMenu(MenuVo menuVo) {
        menuService.trueDelById(menuVo, User.class, RoleMenu.class);
        return CommonResult.ok();
    }

    /**
     * 根据id查找系统菜单信息
     *
     * @param menuVo
     * @return CommonResult.ok(MenuVo)
     */
    @GetMapping("/get")
    @ResponseBody
    @ApiOperation(value = "根据id查找系统菜单信息", notes = "根据id查找系统菜单信息")
    @ApiImplicitParam(value = "menuVo", name = "menuVo", dataType = "org.msfj.admin.vo.MenuVo", paramType = "query")
    public CommonResult getMenu(MenuVo menuVo) {
        Menu menu = menuService.getById(menuVo.getId());
        return CommonResult.ok(menuService.setOtherProperties(menu));
    }

    /**
     * 根据翻页参数、搜索参数查找系统菜单信息
     *
     * @param menuVo
     * @param page   当前第几页
     * @param rows   每页多少条记录
     * @return CommonResult.ok(PageInfo < MenuVo >)
     */
    @GetMapping("/list")
    @ResponseBody
    @ApiOperation(value = "根据翻页参数、搜索参数查找系统菜单信息", notes = "根据翻页参数、搜索参数查找系统菜单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "menuVo", name = "menuVo", dataType = "org.msfj.admin.vo.MenuVo", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "page", dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "rows", value = "rows", dataType = "Integer", paramType = "path")
    })
    public CommonResult findMenuList(MenuVo menuVo, Integer page, Integer rows) {
        PageInfo<MenuVo> menuVoPageInfo = menuService.getByMenu(menuVo, page, rows);
        return CommonResult.ok(menuVoPageInfo);
    }

    /**
     * 根据搜索参数查找所有系统菜单信息
     *
     * @param menuVo
     * @return CommonResult.ok(List < MenuVo >)
     */
    @GetMapping("/getAll")
    @ResponseBody
    @ApiOperation(value = "根据搜索参数查找所有系统菜单信息", notes = "根据搜索参数查找所有系统菜单信息")
    @ApiImplicitParam(value = "menuVo", name = "menuVo", dataType = "org.msfj.admin.vo.MenuVo", paramType = "query")
    public CommonResult getAllMenu(MenuVo menuVo) {
        List<MenuVo> menuVoList = menuService.getAllMenu(menuVo);
        return CommonResult.ok(menuVoList);
    }

    /**
     * 获取系统菜单信息Tree对象
     *
     * @return CommonResult.ok(List < MenuVo >)
     */
    @GetMapping("/getTree")
    @ResponseBody
    @ApiOperation(value = "获取系统菜单信息Tree对象", notes = "获取系统菜单信息Tree对象")
    public CommonResult getMenuTree(boolean onlyMenu) {
        List<MenuVo> menuVos = menuService.getMenuTree(onlyMenu);
        return CommonResult.ok(menuVos);
    }

    /**
     * 获取系统菜单信息级联对象
     *
     * @param id 不可选的id
     * @return CommonResult.ok(List < CascaderResult >)
     */
    @GetMapping("/getCascader")
    @ResponseBody
    @ApiOperation(value = "获取系统菜单信息级联对象", notes = "获取系统菜单信息级联对象")
    @ApiImplicitParam(value = "id", name = "id", dataType = "java.lang.Long", paramType = "query")
    public CommonResult getCascader(String id) {
        List<MenuVo> menuVos = menuService.getMenuTree(false);
        List<CascaderResult> cascaderResults = CascaderResult.getCascaderResult("Id", "Name", "TreePosition", id, menuVos);
        return CommonResult.ok(cascaderResults);
    }
}
